var express = require('express');
var router = express.Router();
let database = require('../db/db');

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', {title: 'Express'});
});
router.get('/list', function (req, res, next) {
  res.json({title: 'hello'});
});
router.get('/user', function (req, res, next) {
  //code: 1 成功  -1 失败
  res.json({code: 1, name: '美女你好呀'});
});
router.get('/getlist', function (req, res, next) {
  //
  //code: 1 成功  -1 失败
  database.query('select * from user', (err, result) => {
    if (err) {
      console.error(err);
      return
    }
    res.json({code: 1, result});
  });

});

router.post('/login', function (req, res, next) {
  let {username, password} = req.body
  switch (username) {
    case 'admin':
      if (password === 'admin') {
        res.send({
          username: 'admin',
          code: 1,
          role_id: 'admin',
          token: 'abc'
        });
      } else {
        res.send({code: -1, message: '登录失败'});
      }
      break;

    case 'viewer':
      if (password === 'viewer') {
        res.send({
          username: 'viewer',
          code: 1,
          role_id: 'viewer',
          token: 'abc'
        });
      } else {
        res.send({code: -1, message: '登录失败'});
      }
      break;


    case 'teacher':
      if (password === 'teacher') {
        res.send({
          username: 'teacher',
          code: 1,
          role_id: 'teacher',
          token: 'abc'
        });
      } else {
        res.send({code: -1, message: '登录失败'});
      }
      break;
  }
});


router.get('/getInfo', function (req, res, next) {
  let {username} = req.query;
  switch (username) {
    case 'admin':
      res.send({
        code: 1,
        role_id: 'admin',
      });
      break;
    case 'viewer':
      res.send({
        code: 1,
        role_id: 'viewer',
      });
      break;
    case 'teacher':
      res.send({
        code: 1,
        role_id: 'teacher',
      });
      break;
    default:
      res.send({
        code: 1,
        role_id: 'viewer',
      });
  }
});


module.exports = router;
